﻿using DAL.comm;
using Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
  public  class BasepurchaseBillDAL
    {
        private SqlCommand cmd;
        public BasepurchaseBillDAL()
        {
            cmd = new SqlCommand();
            cmd.CommandType = CommandType.StoredProcedure;
        }/// <summary>
        /// 按订单编号查询
        /// </summary>
       
        public List <string > FindPurchaseBillIDByPurchaseBillID(string pid)
        {
            cmd.CommandText = "FindPurchaseBillIDByPurchaseBillID";//存储过程名
            cmd.Parameters.Clear();//清空存储过程参数
            cmd.Parameters.Add("@PurchaseID", SqlDbType.NChar, 12).Value = pid;//添加存储过程参数
            List<string> list = new List<string>();//新建string泛型集合用于存储DB读出的记录
            SqlDataReader read = SQLHelper.ExcuteReader(cmd);//读库
            while (read .Read())
            {
                string id = read["PurchaseID"].ToString();//将读出的记录付给变量ID
                list.Add(id);//将ID值添加到泛型集合中

            }
            read.Close();
            return list;
        }
        /// <summary>
        /// 根据订单状态查询订单
        /// </summary>

        public List<string> FindPurchaseBillIDByOnprocess(int  onprocess)
        {
            cmd.CommandText = "FindPurchaseBillIDByOnprocess";//存储过程名
            cmd.Parameters.Clear();//清空存储过程参数
            cmd.Parameters.Add("@Onprocess", SqlDbType.SmallInt).Value = onprocess;//添加存储过程参数
            List<string> list = new List<string>();//新建string泛型集合用于存储DB读出的记录
            SqlDataReader read = SQLHelper.ExcuteReader(cmd);//读库
            while (read.Read())
            {
                string id = read["PurchaseID"].ToString();//将读出的记录付给变量ID
                list.Add(id);//将ID值添加到泛型集合中

            }
            read.Close();
            return list;
        }
        /// <summary>
        /// 根据订单日期查找
        /// </summary>
     
        public List<string> FindPurchaseBillIDByPurchaseDate(DateTime  StartDate, DateTime EndDate)
        {
            cmd.CommandText = "FindPurchaseBillIDByPurchaseDate";//存储过程名
            cmd.Parameters.Clear();//清空存储过程参数
            cmd.Parameters.Add("@startDate", SqlDbType.DateTime).Value = StartDate;//添加存储过程参数
            cmd.Parameters.Add("@endDate", SqlDbType.DateTime).Value = EndDate;//添加存储过程参数
            List<string> list = new List<string>();//新建string泛型集合用于存储DB读出的记录
            SqlDataReader read = SQLHelper.ExcuteReader(cmd);//读库
            while (read.Read())
            {
                string id = read["PurchaseID"].ToString();//将读出的记录付给变量ID
                list.Add(id);//将ID值添加到泛型集合中

            }
            read.Close();
            return list;
        }
        /// <summary>
        ///按业务员查询订单编号  
        /// </summary>
       
        public List<string> FindPurchaseBillIDByClerk(string clerk)
        {
            cmd.CommandText = "FindPurchaseBillIDByClerk";//存储过程名
            cmd.Parameters.Clear();//清空存储过程参数
            cmd.Parameters.Add("@Clerk", SqlDbType.NVarChar, 6).Value = clerk;//添加存储过程参数
            List<string> list = new List<string>();//新建string泛型集合用于存储DB读出的记录
            SqlDataReader read = SQLHelper.ExcuteReader(cmd);//读库
            while (read.Read())
            {
                string id = read["PurchaseID"].ToString();//将读出的记录付给变量ID
                list.Add(id);//将ID值添加到泛型集合中

            }
            read.Close();
            return list;
        }
        /// <summary>
        ///按审核员查询订单编号
        /// </summary>
       
        public List<string> FindPurchaseBillIDByExaminer(string examiner)
        {
            cmd.CommandText = "FindPurchaseBillIDByExaminer";//存储过程名
            cmd.Parameters.Clear();//清空存储过程参数
            cmd.Parameters.Add("@Examiner", SqlDbType.NVarChar, 6).Value = examiner;//添加存储过程参数
            List<string> list = new List<string>();//新建string泛型集合用于存储DB读出的记录
            SqlDataReader read = SQLHelper.ExcuteReader(cmd);//读库
            while (read.Read())
            {
                string id = read["PurchaseID"].ToString();//将读出的记录付给变量ID
                list.Add(id);//将ID值添加到泛型集合中

            }
            read.Close();
            return list;
        }
        /// <summary>
        /// 根据订单编号查找详细订单列表信息
        /// </summary>
       
        public PurchaseBill FindDateilPurchaseBillByPurchaseID(string pid)
        {
            cmd.CommandText = "FindDateilPurchaseBillByPurchaseID";
            cmd.Parameters.Clear();//清空存储过程参数
            cmd.Parameters.Add("@PurchaseID", SqlDbType.NChar, 12).Value = pid;//添加存储过程参数

            List<PurchaseDateil> list = new List<PurchaseDateil>();//新建string泛型集合用于存储DB读出的记录
            SqlDataReader read = SQLHelper.ExcuteReader(cmd);//读库
            while  (read.Read())
            {
                PurchaseDateil  detail= new PurchaseDateil();
                //bo.PurchaseBillID = read["PurchaseID"].ToString().Trim();//将读出的记录付给变量ID
                //bo.SupplierID = read["SupplierID"].ToString().Trim();
                //bo.PurchaseDate = Convert.ToDateTime(read["PurchaseDate"]);
                //bo.StockDate = Convert.ToDateTime(read["StockDate"]);
                //bo.Clerk = read["Clerk"].ToString().Trim();
                //bo.Examiner = read["Examiner"].ToString().Trim();
                //bo.Custodian = read["Custodian"].ToString().Trim();
                //bo.OnProcess = int.Parse(read["OnProcess"].ToString());
                //bo.Memo = read["Memo"].ToString().Trim();
                detail.PurchaseID = read["PurchaseID"].ToString().Trim();
                detail.PurchaseDetailID = Convert.ToInt16(read["PurchaseDatailID"]);
                detail.ProductID = read["ProductID"].ToString().Trim();
                detail.PurchasePrice  = Convert.ToDecimal (read["PurchasePrice"]);
                detail.Quantity =int.Parse(read["Quantity"].ToString ());



                list.Add(detail);

            }
            
            read.Close();
            return FindPurchaseBillByPurchaseID(pid ,list );

        }/// <summary>
         ///根据订单编号查找订单（包含详单列表信息）
         /// </summary>

        public PurchaseBill FindPurchaseBillByPurchaseID(string pid, List<PurchaseDateil> Dateillist)
        {

            cmd.CommandText = "FindPurchaseBillByPurchaseID";
            cmd.Parameters.Clear();//清空存储过程参数
            cmd.Parameters.Add("@PurchaseID", SqlDbType.NChar, 12).Value = pid;//添加存储过程参数
            SqlDataReader reader = SQLHelper.ExcuteReader(cmd);//读库
            if  (reader.Read())
            {
                PurchaseBill bo = new PurchaseBill();
                bo.PurchaseBillID = reader["PurchaseID"].ToString().Trim();//将读出的记录付给变量ID
                bo.SupplierID = reader["SupplierID"].ToString().Trim();
                bo.PurchaseDate = Convert.ToDateTime(reader["PurchaseDate"]);

                if (reader["StockDate"]!=Convert .DBNull)//判断入库日期是否为空
                    bo.StockDate = Convert.ToDateTime(reader["StockDate"]);
                bo.Clerk = reader["Clerk"].ToString().Trim();

                //判断审核员Examiner、仓库管理员Custodian是否为空
                if (reader["Examiner"] != Convert.DBNull || reader["Custodian"] != Convert.DBNull)
                {
                    bo.Examiner = reader["Examiner"].ToString().Trim();
                    bo.Custodian = reader["Custodian"].ToString().Trim();
                }
                bo.OnProcess = int.Parse(reader["OnProcess"].ToString());
                bo.Memo = reader["Memo"].ToString().Trim();
                bo.PurchaseDetailList = Dateillist;
                //detail.ProductID = read["PurchaseID"].ToString().Trim();
                //detail.PurchaseDetailID = Convert.ToInt16(read["PurchaseDetailID"]);
                //detail.ProductID = read["ProductID"].ToString().Trim();
                //detail.PurchasePrice = Convert.ToDecimal(read["PurchasePrice"]);
                //detail.Quantity = int.Parse(read["PurchasePrice"].ToString());



                reader.Close();
                return bo;

            }
            else
            {
                reader.Close();
                return null;
            }
        }
    }
}
